%p Tutorial 3 
%p	Finders:
%p	note: It is not nice at all to write a lot of ruby inside the view. However, I am doing so for simplicity. I read that the best way is to add the method in the model, call it in the controller to keep the controller simple.

%ul
  %li  Model.all
  %P Selects all objects matching the provided conditions applied. There is no need for condition as we saw in the index action where we wrote BlogPosts.all
  %p BlogPost.all(conditions: {content: /^no content/i}). This should return all posts having content that contains the statement "no content".

=@allBlogs = BlogPost.all(conditions: {content: /^no content/i})
- @allBlogs.each do |blog_post|
  %p
  %b= link_to blog_post.title, blog_post 
  %p created at
  %p= blog_post.time
  %p Tags:
  %p= blog_post.tag1
  %p= blog_post.tag2
  %p= blog_post.tag3
  %p Content:
  %p= blog_post.content
  %p id:	
  %p= blog_post.id
  %p{style: "float: right;"}
  = link_to "Delete", blog_post, method: :delete, confirm: "Are you sure you want to delete this post?"

  = link_to "modify", blog_post, method: :put
  %hr{style: "clear: both;"}
%br 
	
%p
  Trying BlogPost.all(sort: [[ :title, :asc ]]):
=@sorted = BlogPost.all(sort: [[ :title, :asc ]])


=@sorted = BlogPost.all(conditions: {content: /^no content/i})
- @sorted.each do |blog_post|
  %p
  %b= link_to blog_post.title, blog_post 
  %p created at
  %p= blog_post.time
  %p Tags:
  %p= blog_post.tag1
  %p= blog_post.tag2
  %p= blog_post.tag3
  %p Content:
  %p= blog_post.content
  %p id:	
  %p= blog_post.id
  %p{style: "float: right;"}
  = link_to "Delete", blog_post, method: :delete, confirm: "Are you sure you want to delete this post?"

  = link_to "modify", blog_post, method: :put
  %hr{style: "clear: both;"}
%br 
%ul  
  %li  Model.count
  %p Returns the number of documents in the database. If you want to specify conditions use where. 
  %p The number of blogposts is (The result is the output of BlogPost.count)
  %p= BlogPost.count

%hr{style: "clear: both;"}
%ul
  %li  Model.exists?
  %p Do any documents exist in the database for the provided conditions?
  %p (The result is the output of BlogPost.exists?)
  %p= BlogPost.exists?
%hr{style: "clear: both;"}
%ul
  %li  Model.find
  %p Provides the ability to find a document or documents given a set of conditions or ids
  %p Examples: Person.find([id_one, id_two])
  %p Person.find(["4baa56f1230048567300485c","4baa56f1230048567300485d"])
  %p Person.where(last_name: "Black").find([ id_one, id_two ])

%hr{style: "clear: both;"}
%ul
  %li  Model.find_or_create_by
  %p Find a document given the provided conditions, or instantiate a new one if nothing is persisted. (7elwa deh! :D) bas 3rfa a-view it :S
-@blog_post  = BlogPost.find_or_initialize_by(title: "title created by find_or_create", content: "content here!")

%hr{style: "clear: both;"}
%ul
  %li  Model.find_or_initialize_by
%hr{style: "clear: both;"}
%ul
  %li  Model.first
  %p return the first element in the database or nil if nothing is found.
-@blog = BlogPost.first
%p= @blog.title
%p
%p= @blog.content
%p you may also specify conditions: BlogPost.first(conditions: { title: "my blog" })
%p I think this finds the first document after sorting BlogPost.first(sort: [[ :title, :asc ]])

%hr{style: "clear: both;"}
%ul
  %li  Model.last
  %p last in stead of first

= link_to 'Back', blog_posts_path

